/*
 *  文件检索
 */

package com.wex.app.tools;

import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.app.Context;
import ohos.data.resultset.ResultSet;
import ohos.media.photokit.metadata.AVStorage;
import ohos.utils.net.Uri;

public class FileFind {

    static public void showResultSet(ResultSet resultSet) {
        if (resultSet == null)
            return;
        while (resultSet.goToNextRow()) {
            try {
                System.out.println(
                "  id:" + resultSet.getInt(resultSet.getColumnIndexForName(AVStorage.AVBaseColumns.ID)) + "\n"
                + "  data:" + resultSet.getString(resultSet.getColumnIndexForName(AVStorage.AVBaseColumns.DATA)) + "\n"
//                    + "  date_added:" + resultSet.getString(resultSet.getColumnIndexForName(AVStorage.AVBaseColumns.DATE_ADDED)) + "\n"
                + "  display:" + resultSet.getString(resultSet.getColumnIndexForName(AVStorage.AVBaseColumns.DISPLAY_NAME)) + "\n"
                + "  duration:" + resultSet.getLong(resultSet.getColumnIndexForName(AVStorage.AVBaseColumns.DURATION)) + "\n"
                + "  mime_type:" + resultSet.getString(resultSet.getColumnIndexForName(AVStorage.AVBaseColumns.MIME_TYPE)) + "\n"
//                    + "  output:" + resultSet.getString(resultSet.getColumnIndexForName(AVStorage.AVBaseColumns.OUTPUT)) + "\n"
                + "  size:" + resultSet.getLong(resultSet.getColumnIndexForName(AVStorage.AVBaseColumns.SIZE)) + "\n"
                + "  title:" + resultSet.getString(resultSet.getColumnIndexForName(AVStorage.AVBaseColumns.TITLE)) + "\n"
                + "  volume:" + resultSet.getString(resultSet.getColumnIndexForName(AVStorage.AVBaseColumns.VOLUME_NAME)) + "\n"
//                    + "  artist:" + resultSet.getString(resultSet.getColumnIndexForName("artist")) + "\n"
//                    + "  album:" + resultSet.getString(resultSet.getColumnIndexForName("album")) + "\n"
                );
            } catch (Exception e) {
                System.out.println("--->> showResultSet error: " + e.toString());
                return;
            }
        }
    }

    /*
     *  uri:
     *      AVStorage.Audio.Media.EXTERNAL_DATA_ABILITY_URI
     *      AVStorage.Video.Media.EXTERNAL_DATA_ABILITY_URI
     *      AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI
     */
    static public ResultSet listXXX(Context context, Uri uri) {
        ResultSet resultSet = null;
        try {
            DataAbilityHelper helper = DataAbilityHelper.creator(context);
            resultSet = helper.query(uri, null, null);
        } catch (IllegalArgumentException | IllegalStateException | NullPointerException | DataAbilityRemoteException e) {
            System.out.println("---->> error : " + e.toString());
            return null;
        }
        return resultSet;
    }

    static public ResultSet listMusic(Context context) {
        return listXXX(context, AVStorage.Audio.Media.EXTERNAL_DATA_ABILITY_URI);
    }

    static public ResultSet listVideo(Context context) {
        return listXXX(context, AVStorage.Video.Media.EXTERNAL_DATA_ABILITY_URI);
    }

    static public ResultSet listImage(Context context) {
        return listXXX(context, AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI);
    }

    static public ResultSet listElse(Context context) {
        return listXXX(context, AVStorage.Downloads.fetchResource("0"));
    }
}
